import { Body, Controller, Get, HttpStatus, Post, Query } from '@nestjs/common';
import { UserService } from './user.service';
import {
  ApiBearerAuth,
  ApiOperation,
  ApiQuery,
  ApiResponse,
  ApiTags,
} from '@nestjs/swagger';
import { PublicVoError, PublicVoSuccess } from 'src/utils/publicVo';
import { RegisterUserDto } from './dto/register-user-dto';
import { LoginUserDto } from './dto/login-user-dto';
import { UserListVo } from './vo/list-user-vo';
import { UserLoginVo } from './vo/login-user-vo';
import { RequireLogin } from 'src/middleware/auth/custom.decorator';

@ApiTags('用户中心')
@Controller('user')
export class UserController {
  constructor(private readonly userService: UserService) {}

  @Get('register/email')
  @ApiOperation({ summary: '邮箱验证码', description: '邮箱验证码接口' })
  @ApiResponse({
    status: HttpStatus.OK,
    description: '请求成功',
    type: PublicVoSuccess,
  })
  @ApiResponse({
    status: HttpStatus.BAD_REQUEST,
    description: '请求失败',
    type: PublicVoError,
  })
  @ApiQuery({
    name: 'email',
    type: String,
    description: '邮箱地址',
    required: true,
  })
  async emailCode(@Query('email') email: string) {
    return await this.userService.emailCode(email);
  }

  @Post('register')
  @ApiOperation({ summary: '注册', description: '注册接口' })
  @ApiResponse({
    status: HttpStatus.OK,
    description: '请求成功',
    type: PublicVoSuccess,
  })
  @ApiResponse({
    status: HttpStatus.BAD_REQUEST,
    description: '请求失败',
    type: PublicVoError,
  })
  async register(@Body() registerUserDto: RegisterUserDto) {
    return await this.userService.register(registerUserDto);
  }

  @Get('login/svg')
  @ApiOperation({ summary: 'svg图形验证码', description: 'svg图形验证码接口' })
  @ApiResponse({
    status: HttpStatus.OK,
    description: '请求成功',
    type: PublicVoSuccess,
  })
  @ApiResponse({
    status: HttpStatus.BAD_REQUEST,
    description: '请求失败',
    type: PublicVoError,
  })
  async svgCode() {
    return await this.userService.svgCode();
  }

  @Post('login')
  @ApiOperation({ summary: '登录', description: '登录接口' })
  @ApiResponse({
    status: HttpStatus.OK,
    description: '请求成功',
    type: UserLoginVo,
  })
  @ApiResponse({
    status: HttpStatus.BAD_REQUEST,
    description: '请求失败',
    type: PublicVoError,
  })
  async login(@Body() loginUserDto: LoginUserDto) {
    return await this.userService.login(loginUserDto);
  }

  @Get()
  @ApiBearerAuth()
  @RequireLogin()
  @ApiOperation({ summary: '用户列表', description: '用户列表接口' })
  @ApiResponse({
    status: HttpStatus.OK,
    description: '请求成功',
    type: UserListVo,
  })
  @ApiResponse({
    status: HttpStatus.BAD_REQUEST,
    description: '请求失败',
    type: PublicVoError,
  })
  @ApiQuery({
    name: 'pageNum',
    type: Number,
    description: '页码',
    required: true,
  })
  @ApiQuery({
    name: 'pageSize',
    type: Number,
    description: '页数',
    required: true,
  })
  @ApiQuery({
    name: 'username',
    type: String,
    description: '用户名',
    required: false,
  })
  @ApiQuery({
    name: 'nickName',
    type: String,
    description: '昵称',
    required: false,
  })
  @ApiQuery({
    name: 'email',
    type: String,
    description: '邮箱',
    required: false,
  })
  async list(
    @Query('username') username: string,
    @Query('nickName') nickName: string,
    @Query('email') email: string,
    @Query('pageNum') pageNum: number,
    @Query('pageSize') pageSize: number,
  ) {
    return await this.userService.list(
      username,
      nickName,
      email,
      pageNum,
      pageSize,
    );
  }
}
